package client;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import metier.modele.Constantes;

/**
 * IHM du client pour la connexion à un serveur
 * @author Quentin, Vincent, Charlie
 *
 */
public class Gestionnaire_Client_Connexion implements ActionListener
{
	
	private JFrame fen;
	private JPanel pseudoPanel, ipPortPanel, buttonPanel;
	private JLabel pseudoL, portL, ipL;
	private JTextField pseudoTF, portTF, ipTF;
	private JButton connexionB, offlineB, quitterB;
	
	int WIDTH = 480;
	int HEIGHT = 250;
	
	/**
	 * Constructeur
	 */
	public Gestionnaire_Client_Connexion() 
	{
		
		fen = new JFrame(Constantes.CLIENT_TITLE);
		fen.setSize(WIDTH, HEIGHT);
		fen.setResizable(false);
		fen.setLocationRelativeTo(null);

		// Gestionnaire de mise en forme.
		GridLayout gl = new GridLayout(3,1);
		fen.getContentPane().setLayout(gl);

		
		pseudoPanel = new JPanel();
		ipPortPanel = new JPanel();
		buttonPanel = new JPanel();
		
		fen.getContentPane().add(pseudoPanel);
		fen.getContentPane().add(ipPortPanel);
		fen.getContentPane().add(buttonPanel);
		
		
		pseudoL = new JLabel("Pseudo : ");
		pseudoL.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		pseudoTF = new JTextField(Constantes.CLIENT_DEFAULT_NICK, 16);
		
		pseudoPanel.add(pseudoL);
		pseudoPanel.add(pseudoTF);
		
		
		portL = new JLabel("Port : ");
		portL.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		portTF = new JTextField(""+Constantes.DEFAULT_PORT, 4);

		ipL = new JLabel("IP : ");
		ipL.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		ipTF = new JTextField(Constantes.CLIENT_DEFAULT_IP, 10);

		ipPortPanel.add(ipL);
		ipPortPanel.add(ipTF);

		ipPortPanel.add(portL);
		ipPortPanel.add(portTF);
		

		connexionB = new JButton("Connexion");
		connexionB.setIcon(new ImageIcon(this.getClass().getResource("/img/icon/icone_acces2.gif")));

		connexionB.addActionListener(this);

		offlineB = new JButton("Offline");
		offlineB.setIcon(new ImageIcon(this.getClass().getResource("/img/icon/network-offline.png")));
		
		offlineB.addActionListener(this);

		quitterB = new JButton("Quitter");
		quitterB.setIcon(new ImageIcon(this.getClass().getResource("/img/icon/logout48.png")));
		
		quitterB.addActionListener(this);

		
		buttonPanel.add(connexionB);
		buttonPanel.add(offlineB);
		buttonPanel.add(quitterB);

		

		fen.setVisible(true);


		fen.addWindowListener (new WindowAdapter()
		{	public void windowClosing (WindowEvent e)
		{ 
			System.exit(0);
		}
		});		

	}


	/**
	 * Détection des clics
	 */
	public void actionPerformed (ActionEvent e)
	{
		Object source =	e.getSource();
				
		//// Connexion au serveur
		if (source == connexionB) 
		{
			int port = Integer.parseInt(portTF.getText());
			if (port > 0 && port < 65535)	// Port valide ?
			{
				String ip = ipTF.getText();
				if (ip.matches(Constantes.IP_REGEX) == true) // IP valide ?
				{
					fen.dispose();

					new Gestionnaire_Client(ip, port, pseudoTF.getText(), true);
						
				}
				else
				{
					JOptionPane.showMessageDialog(null, Constantes.CLIENT_BAD_IP, fen.getTitle(), 0);
				}
			}
			else
			{
				JOptionPane.showMessageDialog(null, Constantes.CLIENT_BAD_PORT_RANGE, fen.getTitle(), 0);
			}
		}
		
		//// Travailler offline
		if (source == offlineB)
		{
			fen.setVisible(false);
			new Gestionnaire_Client("", 0, pseudoTF.getText(), false);
		}
		
		if (source == quitterB) 
		{
			System.exit(0);
		}
	}	
	
}